package com.htthorizon.system.controller;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.htthorizon.common.core.constant.Constants;
import com.htthorizon.common.core.domain.R;
import com.htthorizon.common.log.annotation.Log;
import com.htthorizon.common.log.enums.BusinessType;
import com.htthorizon.system.api.domain.SysLogininfor;
import com.htthorizon.system.api.domain.SysOperLog;
import com.htthorizon.system.service.ISysLogininforService;
import com.htthorizon.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author Brilliant
 */
@RestController
@RequestMapping("/backups")
public class BackUpsController {

    @Autowired
    private ISysLogininforService sysLogininforService;

    @Autowired
    private ISysOperLogService sysOperLogService;

    @GetMapping("/login")
    @Log(title = "系统管理/登录日志备份", businessType = BusinessType.INSERT,type = Constants.SYSTEM)
    public R<String> login(@RequestParam("day") Integer day){
        this.check(day);
        DateTime now = DateTime.now();
        DateTime offset = DateUtil.offset(now, DateField.DAY_OF_YEAR, day);
        List<SysLogininfor> list = sysLogininforService.selectByDate(offset);
        System.out.println(offset);
        sysLogininforService.truncate();
        sysLogininforService.insertBacth(list);
        return R.ok();
    }
    @GetMapping("/restore/login")
    @Log(title = "系统管理/登录日志恢复", businessType = BusinessType.INSERT,type = Constants.SYSTEM)
    public R<String> login2(@RequestParam("day") Integer day){
        this.check(day);
        DateTime now = DateTime.now();
        DateTime offset = DateUtil.offset(now, DateField.DAY_OF_YEAR, day);
        List<SysLogininfor> list = sysLogininforService.selectByDate2(offset);
        System.out.println(offset);
        sysLogininforService.insertBacth2(list);
        return R.ok();
    }

    @GetMapping("/oper")
    @Log(title = "系统管理/操作日志备份", businessType = BusinessType.INSERT,type = Constants.SYSTEM)
    public R<String> oper(@RequestParam("day") Integer day){
        this.check(day);
        DateTime now = DateTime.now();
        DateTime offset = DateUtil.offset(now, DateField.DAY_OF_YEAR, day);
        List<SysOperLog> list =sysOperLogService.selectByDate(offset);
        sysOperLogService.truncate();
        sysOperLogService.insertBacth3(list);
        return R.ok();
    }

    @GetMapping("/restore/oper")
    @Log(title = "系统管理/操作日志恢复", businessType = BusinessType.INSERT,type = Constants.SYSTEM)
    public R<String> oper2(@RequestParam("day") Integer day){
        this.check(day);
        DateTime now = DateTime.now();
        DateTime offset = DateUtil.offset(now, DateField.DAY_OF_YEAR, day);
        List<SysOperLog> list =sysOperLogService.selectByDate2(offset);
        sysOperLogService.insertBacth2(list);
        return R.ok();
    }

    private void check(Integer day){
        if (Objects.isNull(day)){
            throw new RuntimeException("日期不能为空");
        }
    }

}
